<?php

namespace maotool\captcha;

use maotool\util\RandomUtil;

class Captcha
{
    protected $fonts = [
        __DIR__ . '/assets/ttfs/1.ttf',
        __DIR__ . '/assets/ttfs/2.ttf',
        __DIR__ . '/assets/ttfs/3.ttf',
        __DIR__ . '/assets/ttfs/4.ttf',
        __DIR__ . '/assets/ttfs/5.ttf',
        __DIR__ . '/assets/ttfs/6.ttf',
        __DIR__ . '/assets/zhttfs/1.ttf',
    ];
    protected $backgrounds = [
        __DIR__ . '/assets/bgs/1.jpg',
        __DIR__ . '/assets/bgs/2.jpg',
        __DIR__ . '/assets/bgs/3.jpg',
        __DIR__ . '/assets/bgs/4.jpg',
        __DIR__ . '/assets/bgs/5.jpg',
        __DIR__ . '/assets/bgs/6.jpg',
        __DIR__ . '/assets/bgs/7.jpg',
        __DIR__ . '/assets/bgs/8.jpg',
    ];

    protected $key = "ffdade342507824838446f8b4edba88f";

    protected $code;
    protected $width = 150;
    protected $height = 60;
    protected $length = 4;
    protected $generator = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz23456789";

    public function __construct($key = "ffdade342507824838446f8b4edba88f")
    {
        $this->key = $key;
        $this->code = RandomUtil::randomString($this->length, $this->generator);
    }

    public function createCode()
    {
        // 创建图像
        $image = imagecreate($this->width, $this->height);
        imagecolorallocate($image, 255, 255, 255);

        // 创建背景
        $backgroundImage = imagecreatefromjpeg(RandomUtil::randomEle($this->backgrounds));
        imagecopyresampled($image, $backgroundImage, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height);
        imagedestroy($backgroundImage);

        // 创建文字
        $textColor = imagecolorallocate($image, mt_rand(1, 150), mt_rand(1, 150), mt_rand(1, 150));
        $fontPath = RandomUtil::randomEle($this->fonts);
        $font_size = 28;
        $bbox = imagettfbbox($font_size, 0, $fontPath, $this->code);
        $text_width = $bbox[2] - $bbox[0];
        $text_height = $bbox[1] - $bbox[7];
        $x = ($this->width - $text_width) / 2;
        $y = ($this->height - $text_height) / 2 + $text_height;
        imagettftext($image, $font_size, 0, $x, $y, $textColor, $fontPath, $this->code);

        // 在背景图像上添加干扰线（可选）
        for ($i = 0; $i < 10; $i++) {
            imageline($image, rand(0, $this->width), rand(0, $this->height), rand(0, $this->width), rand(0, $this->height), imagecolorallocate($image, mt_rand(1, 150), mt_rand(1, 150), mt_rand(1, 150))); // 灰色干扰线
        }

        ob_start();
        imagejpeg($image, NULL, 90);
        imagedestroy($image);

        $data = new CaptchaData(ob_get_clean(), $this->code);
        $_SESSION['maotool_captcha_' . md5($this->key)] = $data;
        return $data;
    }

    /**
     * @return int
     */
    public function getWidth()
    {
        return $this->width;
    }

    /**
     * @param int $width
     */
    public function setWidth($width)
    {
        $this->width = $width;
    }

    /**
     * @return int
     */
    public function getHeight()
    {
        return $this->height;
    }

    /**
     * @param int $height
     */
    public function setHeight($height)
    {
        $this->height = $height;
    }

    /**
     * @return int
     */
    public function getLength()
    {
        return $this->length;
    }

    /**
     * @param int $length
     */
    public function setLength($length)
    {
        $this->length = $length;
    }

    /**
     * @return string
     */
    public function getCode()
    {
        return $this->code;
    }

    /**
     * @param string $code
     */
    public function setCode($code)
    {
        $this->code = $code;
    }

    /**
     * @return string
     */
    public function getKey()
    {
        return $this->key;
    }

}